
********************************************************************************
********************************************************************************

	**** Placebo test 1: Using unaffected firms (low interest firms) (Table 8)
	
	use "data_RDD_placebo", clear
	
	xtset
	
	* Replacing the average sales data for firms without three consecutive years of sales data:
	gen avgsale1 = L.sale
	gen avgsale2 = (L.sale+L2.sale)/2
	gen avgsale3 = (L.sale+L2.sale+L3.sale)/3
	
	gen avgsale = avgsale3
	replace avgsale = avgsale2 if avgsale==.
	replace avgsale = avgsale1 if avgsale==.
	
	* Sample years: 2016-2019:
	drop if fyear<2016
	gen after=0
	replace after=1 if fyear>=2018
	
	*********************************
	
	gen idit=0	//Data does not report interest income. This biases our results downward.
	
	* Defining high-interest based on all years (if interest expense > (interest income + 0.3 X EBITDA)). 
	gen hiint=0 if interest~=.
	replace hiint=1 if (interest>(idit+0.3*opincome) & interest~=.) //opincome is the corrected version of ebitda
	
	**** ONLY KEEP LOW INTEREST FIRMS
	keep if hiint==0

	* Defining treated based on all years
	gen treated=0 if avgsale~=.
	replace treated=1 if (avgsale>25 & avgsale~=.)
		
	* During 2018-2019 ("after") we want to see if a firm is treated in one year and not in other ("switching")
	gen posttreat= 1 if treated & after
	replace posttreat = 0 if missing(posttreat)
	egen posttreatsum = sum(posttreat), by(newid)
	
	* it is possible that switching firms are firms that are only present in one year and they are not really switching
	egen nafter = sum(after), by(newid)
	tab posttreatsum nafter, missing
	gen switching = (posttreatsum ==1 & nafter==2)	

	
	*********************************
	
	* Constructing main RD variables:
	
	global covariates size q_ind prof tang
	global basedepvar lev inv comstock_c totfinratio
	global othervars sale avgsale treated switching
	global othervars2 totalassetscurrent capital Lsize Lq_ind Lprof Ltang idit opincome gp ndshield_gp interest int_opincome
	
	* Collapsing the panel data to firm level before and after:
	collapse (last) naics naics2 naics4 public (mean)  $basedepvar $depvar2 $covariates $othervars $othervars2, by(newid after)

	* Keep firms that exist in the data both before and after the shock:
	by newid: gen cnt=_N
	keep if cnt==2

	sort newid after
	ds $covariates
	foreach var in $basedepvar `r(varlist)' {
		sort newid after
		by newid: gen D`var'=`var'[_N]-`var'[1]
	}
	
	foreach var in $basedepvar $covariates $othervars $othervars2 {
		sort newid after
		by newid: gen `var'_pre=`var'[1]
	}

	by newid: egen grandavgsale=mean(avgsale)
	forvalues j=1/4 {
		gen salediffP`j'=(grandavgsale - 25)^`j'
	}

	by newid: keep if _n==_N
	
	winsor2 D* $basedepvar $depvar2 $covariates $othervars $othervars2 , replace cuts(3 97)
	
	keep if ~switching
	keep if treated==0 | treated==1

	global RHSP1 c.treated##c.salediffP1
	global RHSP2 c.treated##c.salediffP1 c.treated##c.salediffP2
	global RHSP3 c.treated##c.salediffP1 c.treated##c.salediffP2 c.treated##c.salediffP3

	global keeplist1 treated salediffP1 c.treated#c.salediffP1 Dsize Dq_ind Dprof Dtang Dtang1 _cons
	global keeplist2 treated salediffP1 c.treated#c.salediffP1 salediffP2 c.treated#c.salediffP2 Dsize Dq_ind Dprof Dtang  _cons
	global keeplist3 treated salediffP1 c.treated#c.salediffP1 salediffP2 c.treated#c.salediffP2 salediffP3 c.treated#c.salediffP3 Dsize Dq_ind Dprof Dtang  _cons
	
	destring naics4, replace
	
	****************************************************************************
	
	*** With fixed window:
	
	preserve
	local win 10 //Window = 10	
	keep if avgsale>=25-`win' & avgsale<=25+`win'
	
	foreach var in Dlev Dtotfinratio Dcomstock_c {
		qui reg `var' $RHSP2 Dsize Dq_ind Dprof Dtang i.naics4 , vce(cluster naics2)
			est store `var'_p2c_w`win'
	}
	foreach var in Dinv {
		qui reg `var' $RHSP2 Dq_ind Dprof , vce(cluster naics2)
			est store `var'_p2c_w`win'
	}
	restore
	
	****************************************************************************
	* Exporting Latex table:
	
	* Placebo 1: Using unaffected firms (low interest firms) (Table 8, panel a)
	esttab 	Dlev_p2c_w10 Dcomstock_c_p2c_w10 Dtotfinratio_p2c_w10 Dinv_p2c_w10 ///
			using "LaTex/table_placebo1_w10win.tex", ///
			replace se ar2 nogaps  b(%9.3f) star(* 0.1 ** 0.05 *** 0.01) ///
			keep($keeplist2) order($keeplist2)
				
	****************************************************************************

	** With optimal window:	
	eststo clear
	foreach var in Dlev Dtotfinratio Dcomstock_c {
		rdbwselect `var' avgsale, c(25) p(2) vce(cluster naics2) all kernel(uniform) covs(Dsize Dq_ind Dprof Dtang)
		qui reg `var' $RHSP2 Dsize Dq_ind Dprof Dtang i.naics4  if avgsale>=25-e(h_mserd) & avgsale<=25+e(h_mserd), vce(cluster naics2)
			est store `var'_p2c_opt
	}
	foreach var in Dinv {
		rdbwselect `var' avgsale, c(25) p(2) vce(cluster naics2) all kernel(uniform) covs(Dq_ind Dprof)
		qui reg `var' $RHSP2 Dq_ind Dprof i.naics4 if avgsale>=25-e(h_mserd) & avgsale<=25+e(h_mserd), vce(cluster naics2)
			est store `var'_p2c_opt
	}
			
	****************************************************************************
	* Exporting Latex table:
	
	* Placebo 1: Using unaffected firms (low interest firms) (Table 8, panel b)
	esttab 	Dlev1_p2c_opt Dcomstock_c_p2c_opt Dtotfinratio_p2c_opt Dinv_p2c_opt ///
			using "LaTex/table_placebo1_optwin.tex", ///
			replace se ar2 nogaps  b(%9.3f) star(* 0.1 ** 0.05 *** 0.01) ///
			keep($keeplist2) order($keeplist2)
			
	****************************************************************************
	****************************************************************************

	**** Placebo test 2: randomizing the treatment threshold (Figure 9)

	tempname placebo
	file open `placebo' using "Placebo_rndcutoff_coeff.txt", text write replace
	file write `placebo' "iteration" " " "cutoff" " " "Dlev1" " " "N_Dlev1" " " "Dtotfinratio" " " "N_Dtotfinratio" " " "Dinv" "N_Dinv" " " _n
	set seed 101010

	forvalues iteration = 1/5000 {

		* use cleaned data: this file is created in "master_RDD.do"
		use "data_RDD_placebo.dta", clear
		xtset newid fyear
		
		gen avgsale1 = L.sale
		gen avgsale2 = (L.sale+L2.sale)/2
		gen avgsale3 = (L.sale+L2.sale+L3.sale)/3
		
		gen avgsale = avgsale3
		replace avgsale = avgsale2 if avgsale==.
		replace avgsale = avgsale1 if avgsale==.
		
		drop if fyear<2016
		gen after=0
		replace after=1 if fyear>=2018
		
		*********************************
		
		gen idit=0	//Data does not report interest income. This biases our results downward.
		
		* Defining high-interest based on all years (if interest expense > (interest income + 0.3 X EBITDA)). 
		gen hiint=0 if interest~=.
		replace hiint=1 if (interest>(idit+0.3*opincome) & interest~=.) //opincome is the corrected version of ebitda
		keep if hiint==1

		* randomizing the treatment cutoff:
		drop if avgsale<=25
		scalar rndcutoff=runiform(50,100)
		
		gen treated=0 if avgsale~=.
		replace treated=1 if (avgsale>rndcutoff & avgsale~=.)
		
		* During 2018-2019 ("after") we want to see if a firm is treated in one year and not in other ("switching")
		gen posttreat= 1 if treated & after
		replace posttreat = 0 if missing(posttreat)
		egen posttreatsum = sum(posttreat), by(newid)
		
		* it is possible that switching firms are firms that are only present in one year and they are not really switching
		egen nafter = sum(after), by(newid)
		tab posttreatsum nafter, missing
		gen switching = (posttreatsum ==1 & nafter==2)
		
		global covariates size q_ind prof tang
		global basedepvar lev inv comstock_c totfinratio
		global othervars sale avgsale treated switching
		global othervars2 totalassetscurrent capital Lsize Lq_ind Lprof Ltang idit opincome gp ndshield_gp interest int_opincome
		
		* Collapsing the panel data to firm level before and after:
		collapse (last) naics naics2 naics4 public (mean)  $basedepvar $depvar2 $covariates $othervars $othervars2, by(newid after)

		* Keep firms that exist in the data both before and after the shock:
		by newid: gen cnt=_N
		keep if cnt==2

		sort newid after
		ds $covariates
		foreach var in $basedepvar `r(varlist)' {
			sort newid after
			by newid: gen D`var'=`var'[_N]-`var'[1]
		}
		
		foreach var in $basedepvar $covariates $othervars $othervars2 {
			sort newid after
			by newid: gen `var'_pre=`var'[1]
		}

		by newid: egen grandavgsale=mean(avgsale)
		forvalues j=1/4 {
			gen salediffP`j'=(grandavgsale - 25)^`j'
		}

		by newid: keep if _n==_N
		
		winsor2 D* $basedepvar $depvar2 $covariates $othervars $othervars2 , replace cuts(3 97)
		
		keep if ~switching
		keep if treated==0 | treated==1
		
		global RHSP2 c.treated##c.salediffP1 c.treated##c.salediffP2
		
		local win 25
		qui reg Dlev $RHSP2 Dsize Dq_ind Dprof Dtang i.naics4 if avgsale>=rndcutoff-`win' & avgsale<=rndcutoff+`win', vce(cluster naics2)
		scalar beta_Dlev1=_b[treated]
		scalar n_Dlev1 = e(N)
		
		local win 25
		qui reg Dinv $RHSP2 Dq_ind Dprof i.naics4 if avgsale>=rndcutoff-`win' & avgsale<=rndcutoff+`win', vce(cluster naics2)
		scalar beta_Dinv=_b[treated]
		scalar n_Dinv = e(N)
					
		* save coefficients in the file:
		file write `placebo' (`iteration') " " (rndcutoff) " " (beta_Dlev1) " " (n_Dlev1) " " (beta_Dtotfinraio) " " (n_Dtotfinratio) " " (beta_Dinv) " " (n_Dinv) _n
		
		display "Iteration number: " `iteration'
	}
	file close `placebo'
	
	import delimited using "Placebo_rndcutoff_coeff.txt", delimiters(" ", collapse) varnames(1) numericcols(_all) asfloat clear
	
	global s s1mono
	
	****************************************************************************
	
	* Exporting Figures:
	* Placebo test 2: randomizing the treatment threshold (Figure 9)
	
	twoway (scatteri 0 -.096 0.08 -.096 , recast(line) lp(dash) lc(red)) (histogram dlev, fraction /*width(0.0175)*/) , xlabel(-0.15(0.03)0.18) ///
		xtitle(Treatment effect on {&Delta}debt ratio) ytitle(Fraction) ylabel(,grid) name(fig_placebo2_Dlev, replace) scheme($s ) legend(pos(6) col(2) label(1 "Baseline estimate") label(2 "Distribution of placebo estimates")) //aspectratio(0.75, placement(c))
		graph export "fig_placebo2_Dlev.png", as(png) replace
				
	twoway (scatteri 0 -.032 0.1 -.032, recast(line) lp(dash) lc(red)) (histogram dinv, fraction /*width(0.0175)*/), xlabel(-0.04(0.01)0.03) ///
		xtitle(Treatment effect on {&Delta}investment rate) ytitle(Fraction)  ylabel(,grid) name(fig_placebo2_Dinv, replace) scheme($s ) legend(pos(6) col(2) label(1 "Baseline estimate") label(2 "Distribution of placebo estimates"))  //aspectratio(0.75, placement(c))
		graph export "fig_placebo2_Dinv.png", as(png) replace
	
	
	****************************************************************************
	****************************************************************************

	**** Placebo test 3: Using a pseudo-treatment year (Table 9)
	
	* using the version on local drive for fast processing
	use "C:\My Data Work\Tax Paper Temp\data_RDD_placebo.dta", clear
	
	xtset
	
	gen avgsale1 = L.sale
	gen avgsale2 = (L.sale+L2.sale)/2
	gen avgsale3 = (L.sale+L2.sale+L3.sale)/3
	
	gen avgsale = avgsale3
	replace avgsale = avgsale2 if avgsale==.
	replace avgsale = avgsale1 if avgsale==.
	
	*********************************
	
	* FOR TREATMENT WINDOW: 2012--2015
	drop if fyear<2012 & fyear>2015
	gen after=0
	replace after=1 if fyear>=2014
	
	*********************************
	
	gen idit=0	//Data does not report interest income. This biases our results downward.
	
	* Defining high-interest based on all years (if interest expense > (interest income + 0.3 X EBITDA)). 
	gen hiint=0 if interest~=.
	replace hiint=1 if (interest>(idit+0.3*opincome) & interest~=.) //opincome is the corrected version of ebitda
	keep if hiint==1

	* Defining treated based on all years
	gen treated=0 if avgsale~=.
	replace treated=1 if (avgsale>25 & avgsale~=.)	
	
	* During ("after") we want to see if a firm is treated in one year and not in other ("switching")
	gen posttreat= 1 if treated & after
	replace posttreat = 0 if missing(posttreat)
	egen posttreatsum = sum(posttreat), by(newid)
	
	* it is possible that switching firms are firms that are only present in one year and they are not really switching
	egen nafter = sum(after), by(newid)
	tab posttreatsum nafter, missing
	gen switching = (posttreatsum ==1 & nafter==2)
		
	tab switching , missing
	
	global covariates size q_ind prof tang
	global basedepvar lev inv comstock_c totfinratio
	global othervars sale avgsale treated switching
	global othervars2 totalassetscurrent capital Lsize Lq_ind Lprof Ltang idit opincome gp ndshield_gp interest int_opincome
	
	* Collapsing the panel data to firm level before and after:
	collapse (last) naics naics2 naics4 public (mean)  $basedepvar $depvar2 $covariates $othervars $othervars2, by(newid after)

	* Keep firms that exist in the data both before and after the shock:
	by newid: gen cnt=_N
	keep if cnt==2

	sort newid after
	ds $covariates
	foreach var in $basedepvar `r(varlist)' {
		sort newid after
		by newid: gen D`var'=`var'[_N]-`var'[1]
	}
	
	foreach var in $basedepvar $covariates $othervars $othervars2 {
		sort newid after
		by newid: gen `var'_pre=`var'[1]
	}

	by newid: egen grandavgsale=mean(avgsale)
	forvalues j=1/4 {
		gen salediffP`j'=(grandavgsale - 25)^`j'
	}

	by newid: keep if _n==_N
	
	winsor2 D* $basedepvar $depvar2 $covariates $othervars $othervars2 , replace cuts(3 97)
	
	keep if ~switching
	keep if treated==0 | treated==1

	global RHSP1 c.treated##c.salediffP1
	global RHSP2 c.treated##c.salediffP1 c.treated##c.salediffP2
	global RHSP3 c.treated##c.salediffP1 c.treated##c.salediffP2 c.treated##c.salediffP3

	global keeplist1 treated salediffP1 c.treated#c.salediffP1 Dsize Dq_ind Dprof Dtang Dtang1 _cons
	global keeplist2 treated salediffP1 c.treated#c.salediffP1 salediffP2 c.treated#c.salediffP2 Dsize Dq_ind Dprof Dtang  _cons
	global keeplist3 treated salediffP1 c.treated#c.salediffP1 salediffP2 c.treated#c.salediffP2 salediffP3 c.treated#c.salediffP3 Dsize Dq_ind Dprof Dtang  _cons
	
	destring naics4, replace
	
	*****************************************************

	*** With fixed window:
	
	** Window = 10
	
	preserve
	local win 10
	keep if avgsale>=25-`win' & avgsale<=25+`win'
	
	foreach var in Dlev1 Dtotfinratio Dcomstock_c {
		qui reg `var' $RHSP2 Dsize Dq_ind Dprof Dtang i.naics4 , vce(cluster naics2)
			est store `var'_p2c_w`win'
	}
	foreach var in Dinv  {
		qui reg `var' $RHSP2 Dq_ind Dprof i.naics4 , vce(cluster naics2)
			est store `var'_p2c_w`win'
	}
	restore

	*****************************************************
	* Exporting Latex table:
	
	* Placebo test 3: Using a pseudo-treatment year (Table 9, panel a)
	esttab 	Dlev1_p2c_w10 Dcomstock_c_p2c_w10 Dtotfinratio_p2c_w10 Dinv_p2c_w10 ///
			using "LaTex/table_placebo3_T2014_w10win.tex", ///
			replace se ar2 nogaps  b(%9.3f) star(* 0.1 ** 0.05 *** 0.01) ///
			keep($keeplist2) order($keeplist2)

	*****************************************************

	** With optimal window:	
	eststo clear
	foreach var in Dlev  Dtotfinratio Dcomstock_c {
		rdbwselect `var' avgsale, c(25) p(2) vce(cluster naics2) all kernel(uniform) covs(Dsize Dq_ind Dprof Dtang)
		qui reg `var' $RHSP2 Dsize Dq_ind Dprof Dtang i.naics4 if avgsale>=25-e(h_cerrd) & avgsale<=25+e(h_cerrd), vce(cluster naics2)
			est store `var'_p2c_opt
	}
	foreach var in Dinv {
		rdbwselect `var' avgsale, c(25) p(2) vce(cluster naics2) all kernel(uniform) covs(Dq_ind Dprof)
		qui reg `var' $RHSP2 Dq_ind Dprof i.naics4 if avgsale>=25-e(h_cerrd) & avgsale<=25+e(h_cerrd), vce(cluster naics2)
			est store `var'_p2c_opt
	}

	*****************************************************
	* Exporting Latex table:
	
	* Placebo test 3: Using a pseudo-treatment year (Table 9, panel b)
	esttab 	Dlev_p2c_opt Dcomstock_c_p2c_opt Dtotfinratio_p2c_opt Dinv_p2c_opt ///
			using "LaTex/table_placebo3_T2014_optwin.tex", ///
			replace se ar2 nogaps  b(%9.3f) star(* 0.1 ** 0.05 *** 0.01) ///
			keep($keeplist2) order($keeplist2)

	*****************************************************

	
					
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	


